百度通用文字识别 “error

您所在的位置:网站首页 param error(210009) 百度通用文字识别 “error

百度通用文字识别 “error

2024-07-13 07:12| 来源: 网络整理| 查看: 265

完完全全的使用最基本的官方示例代码。

“error_code”:216101

get_file_content_as_base64()的使用

image = get_file_content_as_base64("C:\fakepath\0110.bmp") payload = 'image='+image “error_code”:216201

按照上面的写法的话,就会跳入216201的错误,提示格式不对。 对比在官方示例代码上传的图片和自己用get_file_content_as_base64()转换的base64格式。官方示例里面所有的符号显示,比如:/=+,是显示为%2F,%3D,%2B。 所以再转一下符号格式。太菜了,就用最容易想到的方法。。。

image = get_file_content_as_base64(image_input_fullname).replace('/', '%2F').replace('=', '%3D').replace('+', '%2B')

官方示例的完整代码大概就是

import base64 import requests API_KEY = "**********" SECRET_KEY = "***************" def main(): url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + get_access_token() # image 可以通过 get_file_content_as_base64("C:\fakepath\0110.bmp") 方法获取 image = get_file_content_as_base64(image_input_fullname).replace('/', '%2F').replace('=', '%3D').replace('+', '%2B') payload = 'image='+image headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) print(response.text) def get_file_content_as_base64(path): """ 获取文件base64编码 :param path: 文件路径 :return: base64编码信息 """ with open(path, "rb") as f: return base64.b64encode(f.read()).decode("utf8") def get_access_token(): """ 使用 AK,SK 生成鉴权签名(Access Token) :return: access_token,或是None(如果错误) """ url = "https://aip.baidubce.com/oauth/2.0/token" params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY} return str(requests.post(url, params=params).json().get("access_token")) if __name__ == '__main__': main()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3